package com.example.springbootdemo.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springbootdemo.bean.User;
import com.example.springbootdemo.entity.BankAccount;
import com.example.springbootdemo.mapper.BankAccountMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "测试接口")
public class IndexController {

    @RequestMapping
    public String hello(String name){
        return "你好 " + name;
    }

    @Value("${demo.my-name}")  // Spring EL
    private String myName;

    @Value("#{${demo.age} + 100}")
    private int age;

//    @Value("${demo.schools}")
//    List<String> schools;

    @GetMapping("myinfo")
    @ApiOperation(value = "获取用户信息")
    public String myinfo(){
        return myName + ":" + age;
    }

    @Resource
    User user;

    @GetMapping("allInfo")
    public User allInfo(){
        return user;
    }


    @Resource
    BankAccountMapper accountMapper;

    @RequestMapping("queryAccount")
    public List<BankAccount> queryAccount(){
        LambdaQueryWrapper<BankAccount> lqw;
        lqw = new LambdaQueryWrapper<>();
        // 余额 > 10000
        lqw.gt(BankAccount::getBalance, 10000);
        final List<BankAccount> list = accountMapper.selectList(lqw);
        return list;
    }

    @GetMapping("toth")
    public ModelAndView toth(ModelAndView modelAndView){
        modelAndView.setViewName("thymeleaf");
        final List<BankAccount> list = queryAccount();
        // 向视图推送数据
        modelAndView.addObject("accounts", list);
        return modelAndView;
    }

}
